<?php
require_once '../config/config.php';
require_once 'inc/session.php';

$menu = -1;

$id_commande = $_GET['cmd'];

if (isset($_POST['submit'])) {
    if ($_POST['mot_passe'] != $_POST['confirm_mot_passe'])
        $message_erreur = "La confirmation du mot de passe est différente de la mot de passe saisie.";
    else {
        $sql_etablissement = 'SELECT etablissement.id, etablissement, adresse, code_postal, ville FROM etablissement, commande WHERE commande.id_etablissement = etablissement.id AND commande.id = ' . $id_commande;
        $req_etablissement = mysql_query($sql_etablissement) or die(mysql_error());
        $res_etablissement = mysql_fetch_array($req_etablissement);

        $sql = 'INSERT INTO beneficiare (id_commande, nom, prenom, fonction, telephone, email, password, etablissement, adresse, code_postal, ville) VALUES (' . $id_commande . ', "' . $_POST['nom'] . '", "' . $_POST['prenom'] . '", "' . $_POST['fonction'] . '", "' . $_POST['telephone'] . '", "' . $_POST['email'] . '", "' . md5($_POST['password']) . '", "' . $res_etablissement['etablissement'] . '", "' . $res_etablissement['adresse'] . '", "' . $res_etablissement['code_postal'] . '", "' . $res_etablissement['ville'] . '")';
        mysql_query($sql) or die(mysql_error());

        header("location:modifier_abonne.php?cmd=" . $id_commande . '&ajt=1');
        exit();
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Interface Administration | L'essentiel du sup</title>
        <link rel="shortcut icon" href="images/favicon.ico" />
        <link rel="stylesheet" href="css/style.css" type="text/css" />
        <!--[if IE 9]>
            <link rel="stylesheet" media="screen" href="css/ie9.css"/>
        <![endif]-->
        <!--[if IE 8]>
            <link rel="stylesheet" media="screen" href="css/ie8.css"/>
        <![endif]-->
        <!--[if IE 7]>
            <link rel="stylesheet" media="screen" href="css/ie7.css"/>
        <![endif]-->
        <script type="text/javascript" src="js/plugins/jquery-1.7.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.flot.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.flot.resize.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.validate.min.js"></script>
        <script type="text/javascript" src="js/custom/general.js"></script>
        <script type="text/javascript" src="js/plugins/wysiwyg/jquery.wysiwyg.js"></script>
        <script type="text/javascript" src="js/plugins/wysiwyg/wysiwyg.image.js"></script>
        <script type="text/javascript" src="js/plugins/wysiwyg/wysiwyg.link.js"></script>
        <script type="text/javascript" src="js/plugins/wysiwyg/wysiwyg.table.js"></script>
        <script type="text/javascript" src="js/plugins/wysiwyg/wysiwyg.i18n.js"></script>
        <script type="text/javascript" src="js/plugins/wysiwyg/i18n/lang.fr.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery("#validate").validate({
                    rules: {
                        nom: "required",
                        prenom: "required",
                        fonction: "required",
                        email: {
                            required: true,
                            email: true,
                        },
                        mot_passe: "required",
                        confirm_mot_passe: {
                            equalTo: '#mot_passe'
                        }
                    },
                    messages: {
                        nom: "Ce champ est requis",
                        prenom: "Ce champ est requis",
                        fonction: "Ce champ est requis",
                        email: "Adresse email invalide",
                        mot_passe: "Ce champ est requis",
                        confirm_mot_passe: "Mot de passe n'est pas identique"
                    }
                });
            });
        </script>
        <!--[if lt IE 9]>
                <script src="js/css3-mediaqueries.js"></script>
        <![endif]-->
    </head>
    <body class="loggedin">
        <?php include('inc/header.php') ?>
        <div class="mainwrapper">
            <div class="mainwrapperinner">
                <?php include('inc/menu.php') ?>
                <div class="maincontent noright">
                    <div class="maincontentinner">
                        <ul class="maintabmenu">
                            <li class="current">
                                <a>Ajouter un beneficiare</a>
                            </li>
                        </ul>
                        <div class="content">
                            <?php if (!empty($message_succes)) {
                                ?>
                                <div class="notification msgsuccess">
                                    <a class="close"></a>
                                    <p><?php echo $message_succes; ?></p>
                                </div>
                            <?php } ?>
                            <?php if (!empty($message_erreur)) {
                                ?>
                                <div class="notification msgerror">
                                    <a class="close"></a>
                                    <p><?php echo $message_erreur ?></p>
                                </div>
                            <?php } ?>
                            <form id="validate" class="stdform stdform2" action="" method="post">
                                <p>
                                    <label>Nom :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="text" name="nom" id="nom" value="<?php if (isset($_POST["nom"])) echo $_POST['nom'] ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Prénom :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="text" name="prenom" id="prenom" value="<?php if (isset($_POST["prenom"])) echo $_POST['prenom'] ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Fonction :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="text" name="fonction" id="fonction" value="<?php if (isset($_POST["fonction"])) echo $_POST['fonction'] ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Téléphone :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="text" name="telephone" value="<?php if (isset($_POST["telephone"])) echo $_POST['telephone'] ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>E-mail :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="text" name="email" id="email" value="<?php if (isset($_POST["email"])) echo $_POST['email'] ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Mot de passe :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="password" name="mot_passe" id="mot_passe" value="" />
                                    </span>
                                </p>
                                <p>
                                    <label>Confirmation du mot de passe :</label>
                                    <span class="field">
                                        <input class="mediuminput" type="password" name="confirm_mot_passe" id="confirm_mot_passe" value="" />
                                    </span>
                                </p>
                                <p class="stdformbutton">
                                    <button name="submit" class="submit radius2">Enregistrer</button>
                                    <input type="reset" class="reset radius2" value="Annuler" />
                                </p>
                            </form>
                            <br clear="all" />
                        </div>
                    </div>
                    <div class="footer">
                        <p></p>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>